

<title>类型管理</title>
  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">控制台</a>
    <a><cite>类型管理</cite></a>
  </div>
</div>

<div class="layui-fluid">   
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
      </div>
      
      <table id="back-article-type-list" lay-filter="back-article-type-list"></table>  
      <script type="text/html" id="back-article-type-list-operation">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
layui.use('article', layui.factory('article')).use(['admin', 'article', 'table','treeGrid'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form
  ,setter = layui.setter
  ,treeGrid = layui.treeGrid;

  //事件
  var active = {
    add: function(){
      admin.popup({
        title: '添加类型'
        ,area: admin.screen() < 2 ? ['100%','320px'] : ['450px','320px']
        ,id: 'back-article-type-add-popup'
        ,success: function(layero, index){
          view(this.id).render('article/typeform').done(function(){
            // 上级选择
            admin.req({
              url: setter.api_back_article_type_list
              ,done: function(res){
                var list = res.data.list;
                list.unshift({"id":0,"pid":0,"name":"空"})
                $.each(list, function (index, item) {
                  if(item.pid == 0 ){
                    $('#back-article-type-form-type-select').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                  }
                });
                form.render(null, 'back-article-type-form');
              }
            });
            form.on('submit(back-article-type-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              field.status = field.status?1:0;
              admin.req({
                url: setter.api_back_article_type_create
                ,data: field
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭
                  treeGrid.reload('back-article-type-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });
    }
  } 
  $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});

</script>